//
//  CHTableViewController.m
//  CHSimpleUI
//
//  Created by cyptt on 2022/7/14.
//

#import "CHTableViewController.h"
#import "CHSingleChioseView.h"
#import "CHScrollView.h"
#import "CHTabTestViewController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface CHTableViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) CHScrollView *scrollView;
@property (nonatomic, strong) UITableView *tableView1;

@property (nonatomic, strong) UITableView *tableView2;
@property(nonatomic,strong)CHSingleChioseView * singleView;
@end

@implementation CHTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self.view addSubview:self.singleView];
    [self.view addSubview:self.scrollView];
    
    [self.singleView setTitleArr:@[@"0001",@"0002"] defaultColor:UIColor.whiteColor selectedColor:UIColor.redColor lineColor:UIColor.redColor];
    
    [self.singleView btnClickIndexBlockFun:^(NSString * _Nonnull title, int index) {
        NSLog(@"%d",index);
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width*index, 0) animated:YES];
    }];
}



#pragma mark -- UIScrollViewDelegate

//scrollView滚动时，就调用该方法。任何offset值改变都调用该方法。即滚动过程中，调用多次
- (void)scrollViewDidScroll:(UIScrollView *)scrollView; {
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
}
//当scrollView缩放时，调用该方法。在缩放过程中，回多次调用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView API_AVAILABLE(ios(3.2));{
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
}

//当开始滚动视图时，执行该方法。一次有效滑动（开始滑动，滑动一小段距离，只要手指不松开，只算一次滑动），只执行一次。
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;{
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
    
   
    
}

//
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset API_AVAILABLE(ios(5.0));{
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
}

//// 滑动减速时调用该方法
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; {
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
}

//滚动视图减速完成，滚动将停止时，调用该方法。一次有效滑动，只执行一次
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;{
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
    
    int index = 0;
    
    index = (scrollView.contentOffset.x/SCREEN_WIDTH);
   
    [self.singleView setIndex:index];
}

// 当滚动视图动画完成后，调用该方法，如果没有动画，那么该方法将不被调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;{
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
}

//返回将要缩放的UIView对象。要执行多次
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; {
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    view.backgroundColor = UIColor.whiteColor;
    return  view;
}


//当将要开始缩放时，执行该方法。一次有效缩放，就只执行一次
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view API_AVAILABLE(ios(3.2)); {
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
}

//当缩放结束后，并且缩放大小回到minimumZoomScale与maximumZoomScale之间后（我们也许会超出缩放范围），调用该方法

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale; {
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
}

//指示当用户点击状态栏后，滚动视图是否能够滚动到顶部。需要设置滚动视图的属性：
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; {
    
    return YES;
}

//当滚动视图滚动到最顶端后，执行该方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView; {
    NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
}

-(CHScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[CHScrollView alloc] initWithFrame:CGRectMake(0, 140, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _scrollView.backgroundColor = UIColor.redColor;
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES; //整夜翻
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH *2, 0);
        [_scrollView addSubview:self.tableView1];
        CHTabTestViewController * vc = [[CHTabTestViewController alloc]init];
        
        [_scrollView addSubview:vc.view];
    }
    return _scrollView;
}


-(UITableView *)tableView1{
    if (!_tableView1) {
        _tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, self.scrollView.frame.size.height)];
        _tableView1.showsHorizontalScrollIndicator = NO;
        _tableView1.showsVerticalScrollIndicator = NO;
        _tableView1.backgroundColor = UIColor.yellowColor;
  
    }
    return _tableView1;
}

-(UITableView *)tableView2{
    if (!_tableView2) {
        _tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0,SCREEN_WIDTH, self.scrollView.frame.size.height)];
        _tableView2.showsHorizontalScrollIndicator = NO;
        _tableView2.showsVerticalScrollIndicator = NO;
        _tableView2.backgroundColor = UIColor.purpleColor;
      
    }
    return _tableView2;
}

-(CHSingleChioseView *)singleView{
    if (!_singleView) {
        _singleView = [[CHSingleChioseView alloc]initWithFrame:CGRectMake(0, 100, 400, 40)];
        _singleView.backgroundColor = UIColor.grayColor;
        
    }
    return  _singleView;
}


@end
